package com.so.np2.algs;

import java.awt.Desktop;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class WorkbookImpl {
	CellStyle titleStyle;
	CellStyle titleStyleCentered;
	CellStyle resultStyle;
	List<HitResult> results;
	int q1;
	int q2;
	int bitR;
	String pathArquivo = "report.xls";
	Workbook xls;

	WorkbookImpl(List<HitResult> results, int q1, int q2, int bitR) {
		super();
		this.results = results;
		this.q1 = q1;
		this.q2 = q2;
		this.bitR = bitR;
	}

	Workbook getXLS() {
		xls = new HSSFWorkbook();

		Sheet sheet = xls.createSheet("Titulo");

		final String[] series = new String[MainFrame.NUMERO_ALGORITMOS + 1];
		int k = 0;
		series[k++] = "Frames";
		series[k++] = FIFO.getName();
		series[k++] = Random.getName();
		series[k++] = LFU.getName();
		series[k++] = MFU.getName();
		series[k++] = Best.getName();
		series[k++] = SecondChance.getName();
		series[k++] = MRU.getName();
		series[k++] = NUR.getName();

		Row row;
		row = sheet.createRow(0);
		Cell cell;
		for (int i = 0; i < series.length; i++) {
			cell = row.createCell(i);
			cell.setCellValue(series[i]);
		}

		int j = 1;
		for (HitResult resultado : results) {

			row = sheet.createRow(j++);
			cell = row.createCell(0);
			cell.setCellValue(String.valueOf(resultado.frame));
			for (int x = 1; x < series.length; x++) {
				cell = row.createCell(x);
				cell.setCellValue(resultado.getResult(series[x]));
			}
		}
		return xls;
	}

	void exportar(String path) throws IOException {
		File file = new File(pathArquivo);
		FileOutputStream fos = new FileOutputStream(file);
		DataOutputStream dos = new DataOutputStream(fos);

		xls.write(dos);

		dos.close();
		fos.close();
		JOptionPane.showMessageDialog(null,
				"Select where to export the report.xls file.");
		Desktop.getDesktop().open(file);
	}

}
